package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Student;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class StuClaTab
 */
@WebServlet("/StuClaTab")
public class StuClaTab extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StuClaTab() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/json;charset=utf-8");
		String g_id = request.getParameter("g_id");
		String stu_id = (String)request.getSession().getAttribute("stu_id");
		Student stu = new Student();
		try {
			stu = stu.StuClaTab(stu_id, g_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(tea.getStudent().get(0));
		//System.out.println(size);
		System.out.println(stu.getCourse().size());
		JSONArray jsons=new JSONArray();
		for(int i = 0; i<stu.getCourse().size();i++)
		{
			jsons.add(stu.getCourse().get(i));
		}
		//System.out.println(size);
		System.out.println(jsons.toString());
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(jsons.toString());
		out.flush();
		out.close();
	}

}
